/**
 * 
 */
package no.hig.ekm.ircclient;

import java.awt.Point;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 * This class takes care of adding server information, network information, name, nick,
 *  nick2 and port. Adding manually input of server to local file and saves them. It also 
 *  can edit the already saved information.
 * 
 * @author Kristofers
 */
public class AddEditDialog extends JDialog {

    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JLabel lblAddress;
    private JLabel lblGroup;
    private JLabel lblName;
    private JLabel lblPortRange;
    
    private JTextField txtAddress;
    private JTextField txtGroup;
    private JTextField txtName;
    private JTextField txtPortRange;
    
    private JButton btnCancel;
    private JButton btnOK;
    
    private JPanel panButtons;
    //private javax.swing.Box.Filler filler1;
    
    private ArrayList<String> values = new ArrayList<String>();
    //A list of input fields
    private ArrayList<JTextField> inpList = new ArrayList<JTextField>();
    
	/**
	 * Default constructor
	 */
	public AddEditDialog() {
		GenLayout();
		//this.pack();
		this.setVisible(true);
	}
	
	/**
	 * Method for generating layout
	 */
	private void GenLayout() {
		java.awt.GridBagConstraints gridBagConstraints;

        lblName = new JLabel();
        txtName = new JTextField();
        lblAddress = new JLabel();
        txtAddress = new JTextField();
        lblPortRange = new JLabel();
        txtPortRange = new JTextField();
        lblGroup = new JLabel();
        txtGroup = new JTextField();
        //filler1 = new Box.Filler(new java.awt.Dimension(0, 0), new java.awt.Dimension(0, 0), new java.awt.Dimension(32767, 0));
        panButtons = new JPanel();
        btnOK = new JButton();
        btnCancel = new JButton();
        

        
        this.setTitle("Add/edit IRC servers");
        this.setLocationByPlatform(true);
        this.setModal(true);
        this.setResizable(false);
        this.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        //this.setName("diaAddEdit"); // NOI18N
        
        java.awt.GridBagLayout jDialog1Layout = new java.awt.GridBagLayout();
        jDialog1Layout.columnWidths = new int[] {0, 5, 0, 5, 0, 5, 0};
        jDialog1Layout.rowHeights = new int[] {0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0, 5, 0};
        this.getContentPane().setLayout(jDialog1Layout);

        lblName.setText("Name");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        this.getContentPane().add(lblName, gridBagConstraints);

        txtName.setColumns(12);
        txtName.setToolTipText("The name of the server");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.ipadx = 96;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        this.getContentPane().add(txtName, gridBagConstraints);

        lblAddress.setText("Address");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        this.getContentPane().add(lblAddress, gridBagConstraints);

        txtAddress.setColumns(12);
        txtAddress.setToolTipText("The address of the server");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.ipadx = 96;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        this.getContentPane().add(txtAddress, gridBagConstraints);

        lblPortRange.setText("Port range");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 6;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        this.getContentPane().add(lblPortRange, gridBagConstraints);

        txtPortRange.setColumns(12);
        txtPortRange.setToolTipText("Server port range");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 6;
        gridBagConstraints.ipadx = 96;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        this.getContentPane().add(txtPortRange, gridBagConstraints);

        lblGroup.setText("Group");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 8;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        this.getContentPane().add(lblGroup, gridBagConstraints);

        txtGroup.setColumns(12);
        txtGroup.setToolTipText("Server belonging to this network group");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 8;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.ipadx = 96;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        this.getContentPane().add(txtGroup, gridBagConstraints);
        
        inpList.add(txtName);
        inpList.add(txtAddress);
        inpList.add(txtPortRange);
        inpList.add(txtGroup);
        
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 12;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        //this.getContentPane().add(filler1, gridBagConstraints);

        panButtons.setLayout(new java.awt.GridLayout(1, 0, 5, 0));

        btnOK.setText("OK");
        panButtons.add(btnOK);
        
        btnOK.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				//Some simple input verification before saving values
				String buf = null;
				//Iterate through a list of input fields
				for (JTextField  jtf : inpList) {
					buf = jtf.getText();
					if (!buf.isEmpty()) {
						values.add(buf);
					} else {
						JOptionPane.showMessageDialog(null,
								jtf.getToolTipText() + " cannot be empty!",
							    "Input error",
							    JOptionPane.ERROR_MESSAGE);
						System.err.printf("%s cannot be empty! \n",jtf.getToolTipText());
						values.clear();
						break;
					}
				}
				//If the values has been added, exit
				if (!values.isEmpty())
					dispose();       		
            }
        });

        btnCancel.setText("Cancel");
        panButtons.add(btnCancel);
        btnCancel.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
        		dispose();
            }
        });
        
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 12;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        this.getContentPane().add(panButtons, gridBagConstraints);

        //Display the modal window over the connection dialog
		Point parentLoc = Main.win.getLocation();
		Point parentLoc2 = MainToolbar.cd.getLocation();
		int x = parentLoc.x + parentLoc2.x;
		int y = parentLoc.y + parentLoc2.y;
		//System.out.printf("Loc x=%d,y=%d\n",x,y);
		setLocation(x,y);
	}
	
	/**
	 * Constructor for Edit functionality
	 * @param String name
	 * @param address
	 * @param portrange
	 * @param group
	 * @return
	 */
	public AddEditDialog(String sName, String address, String portrange, String group) {

		GenLayout();

		txtName.setText(sName);
		txtAddress.setText(address);
		txtPortRange.setText(portrange);
		txtGroup.setText(group);
		txtGroup.setEnabled(false);
        this.pack();
        this.setVisible(true);
	}
	
	/**
	 * Constructor for Add functionality
	 * @param networkName
	 */
	public AddEditDialog(String networkName) {
		GenLayout();
		txtGroup.setText(networkName);
        this.pack();
        this.setVisible(true);
	}

	/**
	 * Returns the last saved data from input boxes
	 * @return ArrayList<String>
	 */
	public ArrayList<String> getLastVals() {
		return values;
	}
	
}
